package com.ruoyi.system.controller;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.EvCharging;
import com.ruoyi.system.domain.vo.EvChargingStatsVo;
import com.ruoyi.system.service.IEvChargingService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 电动车充电记录Controller
 * 
 * @author camelminger
 * @date 2025-11-01
 */
@RestController
@RequestMapping("/system/chargingofCam")
public class EvChargingController extends BaseController
{
    @Autowired
    private IEvChargingService evChargingService;

    /**
     * 查询电动车充电记录列表
     */
    @PreAuthorize("@ss.hasPermi('system:chargingofCam:list')")
    @GetMapping("/list")
    public TableDataInfo list(EvCharging evCharging)
    {
        startPage();
        List<EvCharging> list = evChargingService.selectEvChargingList(evCharging);
        return getDataTable(list);
    }

    /**
     * 导出电动车充电记录列表
     */
    @PreAuthorize("@ss.hasPermi('system:chargingofCam:export')")
    @Log(title = "电动车充电记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EvCharging evCharging)
    {
        List<EvCharging> list = evChargingService.selectEvChargingList(evCharging);
        ExcelUtil<EvCharging> util = new ExcelUtil<EvCharging>(EvCharging.class);
        util.exportExcel(response, list, "电动车充电记录数据");
    }

    /**
     * 获取电动车充电记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:chargingofCam:query')")
    @GetMapping(value = "/{chargeId}")
    public AjaxResult getInfo(@PathVariable("chargeId") Long chargeId)
    {
        return success(evChargingService.selectEvChargingByChargeId(chargeId));
    }

    /**
     * 新增电动车充电记录
     */
    @PreAuthorize("@ss.hasPermi('system:chargingofCam:add')")
    @Log(title = "电动车充电记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EvCharging evCharging)
    {
        return toAjax(evChargingService.insertEvCharging(evCharging));
    }

    /**
     * 修改电动车充电记录
     */
    @PreAuthorize("@ss.hasPermi('system:chargingofCam:edit')")
    @Log(title = "电动车充电记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EvCharging evCharging)
    {
        return toAjax(evChargingService.updateEvCharging(evCharging));
    }

    /**
     * 删除电动车充电记录
     */
    @PreAuthorize("@ss.hasPermi('system:chargingofCam:remove')")
    @Log(title = "电动车充电记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{chargeIds}")
    public AjaxResult remove(@PathVariable Long[] chargeIds)
    {
        return toAjax(evChargingService.deleteEvChargingByChargeIds(chargeIds));
    }

    /**
     * 充电记录统计
     */
    @PreAuthorize("@ss.hasPermi('system:chargingofCam:list')")
    @GetMapping("/statistics")
    public AjaxResult statistics(@RequestParam(value = "beginTime", required = false) String beginTime,
                                 @RequestParam(value = "endTime", required = false) String endTime,
                                 @RequestParam(value = "granularity", required = false, defaultValue = "day") String granularity)
    {
        Date beginDate = DateUtils.parseDate(beginTime);
        Date endDate = DateUtils.parseDate(endTime);
        List<EvChargingStatsVo> stats = evChargingService.selectChargingStatistics(beginDate, endDate, granularity);
        return success(stats);
    }
}
